using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace GuavaLib
{
    public partial class GLaunchForm : Form
    {
        public GLaunchForm(string value)
        {
            InitializeComponent();
            if (value.Contains("~"))
            {
                // We have a loaded module\
                SetLoaded();
                string[] ls = value.Split('~');
                this.tbFileName.Text = ls[0];
                this.ShowClassesFromFile();
                this.lbClasses.SelectedItem = ls[1];
            }
            else
            {
                // It's from the current module
                SetCurrent();
                txtLocal.Text = value;
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            fileDlg.Filter = "Assembly DLL files (*.dll)|*.dll|Assembly EXE files (*.exe)|*.exe|All files (*.*)|*.*";
            fileDlg.Title = "Select forms assembly name";
            fileDlg.FileName = (string)this.tbFileName.Text;

            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                tbFileName.Text = fileDlg.FileName;
                this.tbFileName.Text = fileDlg.FileName;
                ShowClassesFromFile();
            }
        }

        public void ShowClassesFromFile()
        {
            string s = this.tbFileName.Text;
            if ((s == null) || (s == "")) return;
            
            Assembly asm = Assembly.LoadFile(s);
            Type[] asmTypes = asm.GetTypes();
            lbClasses.Items.Clear();
            foreach (Type t in asmTypes)
            {
                if (t.BaseType.Name == "GForm") lbClasses.Items.Add(t.Name);
            }
            asm = null;
        }

        public string GetSelectedClassName()
        {
            if (this.rbCurrent.Checked) return txtLocal.Text;
            else return this.tbFileName.Text + "~" + lbClasses.SelectedItem.ToString();
        }

        private void rbCurrent_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCurrent.Checked) SetCurrent();
            else SetLoaded();
        }

        private void rbLoaded_CheckedChanged(object sender, EventArgs e)
        {
            if (rbLoaded.Checked) SetLoaded();
            else SetCurrent();
        }

        private void SetCurrent()
        {
            this.tbFileName.Enabled = false;
            this.btnBrowse.Enabled = false;
            this.txtLocal.Enabled = true;
            this.lbClasses.Enabled = false;
            this.lbClasses.Items.Clear();
        }

        private void SetLoaded()
        {
            this.tbFileName.Enabled = true;
            this.btnBrowse.Enabled = true;
            this.txtLocal.Enabled = false;
            this.lbClasses.Enabled = true;
            this.ShowClassesFromFile();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}